package org.springblade.modules.platform.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import org.springblade.modules.platform.dto.EquipmentAlarmDTO;
import org.springblade.modules.platform.entity.EquipmentAlarm;
import org.springblade.modules.platform.service.IDeviceTableService;
import org.springblade.modules.platform.service.IEquipmentAlarmService;
import org.springblade.modules.platform.service.IProductModelPropertiesService;
import org.springblade.modules.platform.vo.DeviceTableVO;
import org.springblade.modules.platform.vo.PropertiesVO;
import org.springblade.modules.platform.vo.enums.ConditionEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/service-huateng-sxdb/equiomentalarm")
@Api(value = "", tags = "设备告警")
@Slf4j
public class EquiomentAlarmController {
    private final IEquipmentAlarmService iEquipmentAlarmService;
    private final IProductModelPropertiesService iProductModelPropertiesService;
    private final IDeviceTableService iDeviceTableService;


    @ApiLog("根据设备获取属性下拉框")
    @GetMapping("/attributeListByequipmentTypeId")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据子设备获取属性-下拉框", notes = "设备ID")
    public R<List<PropertiesVO>> attributeListByequipmentTypeId(String equipmentId) {
        List<PropertiesVO> propertiesList = iProductModelPropertiesService.getPropertiesList(equipmentId);
        return R.data(propertiesList);
    }

    /**
     * 自定义分页
     */
    @ApiLog("设备管理-设备报警设置接口-分页")
    @GetMapping("/page")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "设备报警接口-分页", notes = "传入equipmentAlarm")
    public R<IPage<EquipmentAlarm>> pageAlarm(EquipmentAlarm equipmentAlarm, Query query) {

        log.info("入参为:{}",equipmentAlarm.getEquipmentId());
        String equipmentId = equipmentAlarm.getEquipmentId();
        BladeUser user = AuthUtil.getUser();
        QueryWrapper<EquipmentAlarm> queryWrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(equipmentId)){
            queryWrapper.lambda().eq(EquipmentAlarm::getEquipmentId,equipmentId);
        }else {
            queryWrapper.lambda().in(EquipmentAlarm::getEquipmentId,user.getDeptId().split(","));
        }

        if(!StringUtils.isEmpty(equipmentAlarm.getEquipmentName())){
            queryWrapper.lambda().like(EquipmentAlarm::getEquipmentName,equipmentAlarm.getEquipmentName());
        }
        if(!StringUtils.isEmpty(equipmentAlarm.getProductId())){
            queryWrapper.lambda().eq(EquipmentAlarm::getProductId,equipmentAlarm.getProductId());
        }
        if(!StringUtils.isEmpty(equipmentAlarm.getProductName())){
            queryWrapper.lambda().eq(EquipmentAlarm::getProductName,equipmentAlarm.getProductName());
        }
        if(!StringUtils.isEmpty(equipmentAlarm.getAlarmStatus())){
            queryWrapper.lambda().eq(EquipmentAlarm::getAlarmStatus,equipmentAlarm.getAlarmStatus());
        }
        if(!StringUtils.isEmpty(equipmentAlarm.getAlarmName())){
            queryWrapper.lambda().like(EquipmentAlarm::getAlarmName,equipmentAlarm.getAlarmName());
        }
        IPage<EquipmentAlarm> page = iEquipmentAlarmService.page(Condition.getPage(query),queryWrapper);
        return R.data(page);
    }

    /**
     * 删除
     */
    @ApiLog("删除")
    @PostMapping("/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "设备报警接口-删除", notes = "传入ids")
    public R removeAlarm(@ApiParam(value = "主键集合", required = true) @RequestParam Integer id) {
        return R.status(iEquipmentAlarmService.removeById(id));
    }
    /**
     * 详情
     */
    @ApiLog("详情")
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入productAlarm")
    public R<EquipmentAlarmDTO> detail(Integer id) {
        EquipmentAlarm byId = iEquipmentAlarmService.getById(id);
        String alarmJson = byId.getAlarmJson();
        if(!StringUtils.isEmpty(alarmJson)){
            EquipmentAlarmDTO equipmentAlarmDTO = JSONObject.parseObject(alarmJson,EquipmentAlarmDTO.class);
            equipmentAlarmDTO.setId(id);
            return R.data(equipmentAlarmDTO);
        }

        return R.data(null);
    }

    @ApiLog("修改")
    @PostMapping("/update")
    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "修改", notes = "传入EquipmentAlarmDTO")
    public R update(@Valid @RequestBody EquipmentAlarmDTO equipmentAlarmDTO) {
        EquipmentAlarm equipmentAlarm = new EquipmentAlarm();
        equipmentAlarm.setEquipmentId(equipmentAlarmDTO.getEquipmentId());
        equipmentAlarm.setEquipmentName(equipmentAlarmDTO.getEquipmentName());
        equipmentAlarm.setCreateTime(System.currentTimeMillis());
        equipmentAlarm.setAlarmName(equipmentAlarmDTO.getAlarmName());
        equipmentAlarm.setAlarmStatus(0);
        equipmentAlarm.setId(equipmentAlarmDTO.getId());
        equipmentAlarm.setAlarmJson(JSONObject.toJSONString(equipmentAlarmDTO));
        EquipmentAlarm byId = iEquipmentAlarmService.getById(equipmentAlarmDTO.getId());
        equipmentAlarm.setIsAlert(byId.getIsAlert());
        return R.status(iEquipmentAlarmService.updateById(equipmentAlarm));
    }

    @ApiLog("开启/关闭")
    @PostMapping("/updateState")
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "启用/停用", notes = "传入id")
    public R updateState(@ApiParam(value = "主键集合", required = true) @RequestParam Integer id,
                         @ApiParam(value = "状态（1：启用；0：停用）", required = true) @RequestParam Integer state) {
        EquipmentAlarm byId = iEquipmentAlarmService.getById(id);
        byId.setAlarmStatus(state);
        return R.status(iEquipmentAlarmService.updateById(byId));
    }
    @ApiLog("条件-下拉框")
    @GetMapping("/conditionList")
    @ApiOperationSupport(order = 14)
    @ApiOperation(value = "条件下拉框")
    public R<List> conditionList() {
        return R.data(ConditionEnum.toList());
    }

    @ApiLog("新增")
    @PostMapping("/save")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "新增", notes = "传入equipmentAlarmDTO")
    public R save(@Valid @RequestBody EquipmentAlarmDTO equipmentAlarmDTO) {
        if(equipmentAlarmDTO==null){
            return R.fail("数据不能为空");
        }
        iEquipmentAlarmService.remove(Wrappers.<EquipmentAlarm>query().lambda().eq(EquipmentAlarm::getEquipmentId,equipmentAlarmDTO.getEquipmentId()));
        EquipmentAlarm equipmentAlarm = new EquipmentAlarm();
        equipmentAlarm.setEquipmentId(equipmentAlarmDTO.getEquipmentId());
        DeviceTableVO deviceTableVO = iDeviceTableService.selectDeviceById(equipmentAlarmDTO.getEquipmentId());
        equipmentAlarm.setEquipmentName(deviceTableVO.getDeviceName());
        equipmentAlarm.setProductId(deviceTableVO.getProductId());
        equipmentAlarm.setProductName(deviceTableVO.getProductName());
        equipmentAlarm.setCreateTime(System.currentTimeMillis());
        equipmentAlarm.setAlarmName(equipmentAlarmDTO.getAlarmName());
        equipmentAlarm.setAlarmStatus(0);
        equipmentAlarm.setId(equipmentAlarmDTO.getId());
        equipmentAlarm.setAlarmJson(JSONObject.toJSONString(equipmentAlarmDTO));
        equipmentAlarm.setRemark(equipmentAlarmDTO.getRemark());
        equipmentAlarm.setIsAlert(false);
        return R.data(iEquipmentAlarmService.save(equipmentAlarm));
    }
}
